/*
 * Copyright (C) 2012 Stephane Ribas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ribas.stephane;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ribas.stephane.db.DatabaseHelper;

public class AffectionsDetailsActivity extends Activity {
	// variable pour le debug ;-)
	private static final String TAG = "StatusActivity";

	//MENU OPTIONS
	// Button QUIT + SETTINGS
	final private static int optionGroupId = 1;
	final private static int optionRemedesItemId = 1;
	final private static int optionRemedesOrder = 1;
	final private static int optionQuitItemId = 2;
	final private static int optionQuitOrder = 2;
	
	// BDD declaration to force closeDB
	protected SQLiteDatabase db;

	// Liste Detail View Declaration
	protected TextView affectionNom;
	protected TextView affectionDescription;
	protected TextView affectionTraitement;
	protected int nomId;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleFromActivityLabel (R.id.titleAffectionsDetails);  
        setContentView(R.layout.activity_affectionslist_detail_item);
        
        Log.d(TAG, "CLICK on Item");
        
        nomId = getIntent().getIntExtra("NOM_ID", 0);
        SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT _id, Nom, Description, Traitement FROM listeaffections WHERE _id = ?", 
        		new String[]{""+nomId});
        Log.d(TAG, "CLICK on Item - SELECT statement");
      
        if (cursor.getCount() == 1)
        {
        	cursor.moveToFirst();
        
	        affectionNom = (TextView) findViewById(R.id.detailsNom);
	        affectionNom.setText(cursor.getString(cursor.getColumnIndex("Nom")));
	        
	        affectionNom = (TextView) findViewById(R.id.detailsDescription);
	        affectionNom.setText(cursor.getString(cursor.getColumnIndex("Description")));
	        
		    affectionNom = (TextView) findViewById(R.id.detailsTraitement);
	        affectionNom.setText(cursor.getString(cursor.getColumnIndex("Traitement")));
        }
 
    }

	/**
	 */
	// Click Methods

	/**
	 * Handle the click on the home button.
	 * 
	 * @param v View
	 * @return void
	 */

	public void onClickHome (View v)
	{
	    goHome (this);
	}


	/**
	 * Handle the click on the HELP button.
	 * 
	 * @param v View
	 * @return void
	 */

	public void onClickHelp (View v)
	{
	    startActivity (new Intent(getApplicationContext(), HelpActivity.class));
	}
	/**
	 * Handle the click on the About button.
	 * 
	 * @param v View
	 * @return void
	 */

	public void onClickAbout (View v)
	{
	    startActivity (new Intent(getApplicationContext(), AboutActivity.class));
	}

	/**
	 * Handle the click on the Quit button.
	 * 
	 * @param v View
	 * @return void
	 */

	public void onClickQuit (View v)
	{
        Log.d(TAG, "CLICK on finish");
        finish();
	}
	
    
	/**
	 * onDestroy
	 * The final call you receive before your activity is destroyed. 
	 * This can happen either because the activity is finishing (someone called finish() on it, 
	 * or because the system is temporarily destroying this instance of the activity to save space. 
	 * You can distinguish between these two scenarios with the isFinishing() method.
	 *
	 */


	protected void onDestroy ()
	{
        Log.d(TAG, "Destroy");
        if (db != null) db.close();
		super.onDestroy ();
	}


	/**
	 * Go back to the home activity.
	 * 
	 * @param context Context
	 * @return void
	 */

	public void goHome(Context context) 
	{
	    final Intent intent = new Intent(context, HomeoGratuitHomeActivity.class);
	    intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    context.startActivity (intent);
	}

	/**
	 * Use the activity label to set the text in the activity's title text view.
	 * The argument gives the name of the view.
	 *
	 * <p> This method is needed because we have a custom title bar rather than the default Android title bar.
	 * See the theme definitons in styles.xml.
	 * 
	 * @param textViewId int
	 * @return void
	 */

	public void setTitleFromActivityLabel (int textViewId)
	{
	    TextView tv = (TextView) findViewById (textViewId);
	    if (tv != null) tv.setText (getTitle ());
	} // end setTitleText

	/**
	 * Show a string on the screen via Toast.
	 * 
	 * @param msg String
	 * @return void
	 */

	public void toast (String msg)
	{
	    Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
	} // end toast

	/**
	 * Send a message to the debug log and display it using Toast.
	 */
	public void trace (String msg) 
	{
	    Log.d("HOMEOGRATUIT", msg);
	    toast (msg);
	}  
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 * declaration des menus qui apparait lorsque l'on fait OPTION sur le tel.
	 * implementer : Button QUITTER, SETTINGS
	 * reste a faire: faire apparaitre SETTINGS...
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(optionGroupId,optionRemedesItemId, optionRemedesOrder, getString(R.string.optionRemedes));
		return super.onCreateOptionsMenu(menu);
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 *
	 * declaration des actions du menu OPTION
	 * implementer : Button QUITTER
	 * reste a faire: SETTINGS... etc
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case optionRemedesItemId:
			startActivity (new Intent(getApplicationContext(), RemedesActivity.class));
	        break;
		}
		return super.onOptionsItemSelected(item);
	}


}